package projectServlet;

//import java.io.IOException;
//import java.util.ArrayList;

import java.io.IOException;
import java.util.ArrayList;
import java.util.StringTokenizer;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.taglibs.standard.lang.jpath.adapter.ConversionException;
import org.apache.taglibs.standard.lang.jpath.adapter.Convert;

import dao.DAOException;
import dao.EmployeeDAOFactory;
import dao.mysql.EmployeeDAOFactoryImpl;
import dao.mysql.EmployeeDAOImpl;
import dto.*;

@WebServlet("/AdminList")
public class adminlist extends HttpServlet {

	String Urlpath = "/AdminListPreset";

	public adminlist() {
		super();
	}

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		processRequest(request,response);
		

	}



	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		processRequest(request,response);
		
	}
	private void processRequest(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		
		Urlpath = "/AdminListPreset";
		SubProcess(request,response);
		
		RequestDispatcher rd = request.getRequestDispatcher(Urlpath);
		rd.forward(request, response);	
		
	}
	
	private void SubProcess(HttpServletRequest request,			
			HttpServletResponse response) throws ServletException, IOException {
		
		
		String SearchButton = request.getParameter("Search");
		String UpdateButton = request.getParameter("Update");
		
		if(SearchButton != null && SearchButton.equals("Search"))
		{
			EmployeeDAOFactory eFactory = new EmployeeDAOFactoryImpl();
			UserDTO			   user     = new UserDTO();
			
			String SSearchID = (String)request.getParameter("userid");
			if(!ValidateInvliadInput(SSearchID))
					return;
			
			int SearchID     = Integer.parseInt(SSearchID);
			
			try {
				user = eFactory.getEmployeeDAO().findUser(SearchID);
			} catch (DAOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			request.getSession().setAttribute("AfterSearch", true);
			
			request.setAttribute("alluser", user);	
			Urlpath = "/adminlist.jsp";
		}

		if(UpdateButton != null && UpdateButton.equals("Update"))
		{
			EmployeeDAOFactory eDAO  = new EmployeeDAOFactoryImpl();
			
			if((boolean) request.getSession().getAttribute("AfterSearch"))
			{
				String SAnnualLeave  = request.getParameter("AnnualLeave");
				String SMedicalLeave = request.getParameter("MedicalLeave");
				String SUserID		= request.getParameter("UserID");
				
				SAnnualLeave  = SAnnualLeave.trim();
				SMedicalLeave = SMedicalLeave.trim();
				SUserID		  = SUserID.trim();
				
				if(!ValidateInvliadInput(SAnnualLeave) || !ValidateInvliadInput(SMedicalLeave))
					return;
				
				double AnnualLeave  = Double.parseDouble(SAnnualLeave);
				double MedicalLeave = Double.parseDouble(SMedicalLeave);
				int UserID		 = Integer.parseInt(SUserID);
				

				try {
					double CompensationLeave = eDAO.getEmployeeDAO().findUser(UserID).get_compensationLeave();
					eDAO.getEmployeeDAO().updateUserLeaveDays(UserID, AnnualLeave, MedicalLeave,CompensationLeave);
				} catch (DAOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
				
			}
			else
			{
				String[] SAnnualLeave  = request.getParameterValues("AnnualLeaveMulti");
				String[] SMedicalLeave = request.getParameterValues("MedicalLeaveMulti");
				String[] SUserID		  = request.getParameterValues("UserID");
				
				for(int i = 0; i < SUserID.length;i++)
				{
					SAnnualLeave[i]   = SAnnualLeave[i].trim();
					SMedicalLeave[i]  = SMedicalLeave[i].trim();
					SUserID[i]		  = SUserID[i].trim();
					

					if(!ValidateInvliadInput(SAnnualLeave[i]) || !ValidateInvliadInput(SMedicalLeave[i]))
						return;					
					
					double AnnualLeave  = Double.parseDouble(SAnnualLeave[i]);
					double MedicalLeave = Double.parseDouble(SMedicalLeave[i]);
					int UserID		 = Integer.parseInt(SUserID[i]);
		
					
					try {
						double CompensationLeave = eDAO.getEmployeeDAO().findUser(UserID).get_compensationLeave();					
						eDAO.getEmployeeDAO().updateUserLeaveDays(UserID, AnnualLeave, MedicalLeave,CompensationLeave);
					} catch (DAOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
										
				}
				
			}

		}

	}
	
	private boolean ValidateInvliadInput(String inputS)
	{
		StringTokenizer st = new StringTokenizer(inputS,".");
		while(st.hasMoreTokens())
		{
			String Token = 	st.nextToken();		
			for(int i = 0 ; i < Token.length(); i++)
			{
				if(Token.charAt(i) < '0' || Token.charAt(i) > '9')
					return false;
			}
		}


		return true;
		
	}


	
}
